/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.web.controller;

import com.opgea.qms.constraints.SessionConstraints;
import com.opgea.qms.domain.model.JsonModelMap;
import com.opgea.qms.service.DestinationService;
import com.opgea.qms.service.TokenDetailsService;
import com.opgea.qms.web.form.DestinationDTO;
import com.opgea.qms.web.form.TokenDetailsDTO;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Ramesh
 */
@Controller
@RequestMapping("/destination")
public class DestinationController {
    
    
    @Autowired
    private DestinationService destinationService;
    
    @Autowired
    private TokenDetailsService tokenDetailsService;
    
    @RequestMapping(value="create", method= RequestMethod.POST)
    public @ResponseBody
            Map<String, Object> create(DestinationDTO destinationDTO, HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        destinationDTO.setCompanyId(companyId);
        destinationService.update(destinationDTO);
        return JsonModelMap.success().data(destinationDTO.getName()+"! Saved Successfully.");
    }
    
    @RequestMapping(value="destinationList", method= RequestMethod.GET)
    public @ResponseBody 
            Map<String, Object> destinationList(HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        //Long employeeId = (Long) session.getAttribute(SessionConstraints.EMPLOYEE_ALLOTED_DESTINATION.toString());
        
        List<DestinationDTO> destinationList = destinationService.findAllByCompany(companyId);
        return JsonModelMap.success().data(destinationList);
    }
    
    @RequestMapping(value="tokenListByDestination", method=RequestMethod.GET)
    public @ResponseBody Map<String, Object> getTokenList(HttpServletRequest request){
        HttpSession session = request.getSession();
        Long companyId = (Long) session.getAttribute(SessionConstraints.COMPANY_ID.toString());
        Long destinationId = (Long) session.getAttribute(SessionConstraints.EMPLOYEE_ALLOTED_DESTINATION.toString());
        List<TokenDetailsDTO> tokenList = tokenDetailsService.findAllByDestinationId(companyId, destinationId);
        return JsonModelMap.success().data(tokenList);
    }
}
